home *** CD-ROM | disk | FTP | other *** search
- Path: news.iag.net!news
- From: jatmon@iag.net (John R Buchan)
- Newsgroups: comp.lang.c
- Subject: Re: returning a string from a function
- Date: 12 Jan 1996 19:27:03 GMT
- Organization: Internet Access Group, Orlando, Florida
- Message-ID: <4d6cm7$g4r@news.iag.net>
- References: <4d4uh8$q46@mailhost.mwmicro.com>
- NNTP-Posting-Host: pm1-orl25.iag.net
- X-Newsreader: WinVN 0.99.7
-
- In article <4d4uh8$q46@mailhost.mwmicro.com>, aschlies@citynet.net says...
- >
- >I am learning C with the aid of only a book. I am at an inpass in
- >this process. I have a small function that needs to return a string
- >back to the main routine. I have the following prototype:
- >
- >char function_name(char in_string[80])
- >
- >{
- > char value[80];
- >
- > ...value takes on part of the value of the last 10 characters
- > of in_string.
- >
- > return(value);
-
- Your compiler should have complained about this. Your function is defined
- to return a char, but you are trying to return a char pointer.
-
- >}
- >
- >
- >jWhen this function is done, it only returns the first character. I
- >"watched" the program execution and it shows that value indeed has the
- >last 10 characters. Any clues as to why the calling routine only gets
- >the one character??
-
- First, in c you can't pass or return an array (strings are '\0' terminated
- char arrays), only a pointer to an element in it (usually the first). The
- c.l.c faq (Frequently Asked Question) list has a rather detailed discussion
- of this. You might want to read through it. It is available for anonymous
- ftp from rtfm.mit.edu /pub/usenet/comp.lang.c.
-
- If you change your function definition to :
-
- char *function_name(char in_string[80])
-
- it will work (or seem to). Unfortunately, you have just created another
- problem. value is local object with a storage type of automatic. In
- other words, it is allocated (created) when the function is entered and
- deallocated (disappears), when the function exits. So, by returning a
- pointer to it, you are actually returning an invalid pointer (one that
- doesn't point to a valid object).
-
- You can overcome this by defining value with a static storage class.
-
- static char value[80];
-
- This means that there will be one permanent value object. It will continue
- to exist, when the function exits. However, each time you call function_name,
- the prior contents of value will be overwritten.
-
- >
- >Also, I wrote the routine that copies the last 10 characters. Is there
- >a lib. function that does that for me?? Did I reinvent the wheel?
-
- No, there is no standard function. You simply set a pointer to the desired
- location in the string and strcpy the ptr.
-
- #include <stdio.h>
- #include <string.h>
-
- int main()
- {
- char str1[] = "This is a nonsense string to demonstrate something.";
- char buf[11], *ptr;
-
- if( strlen(str1) > 10)
- ptr = str1 + (strlen(str1) - 10);
- else
- ptr = str1;
-
- strcpy( buf, ptr);
- puts( buf);
-
- return 0;
- }
-
- --
- John R Buchan -:|:- Looking for that elusive FAQ? ftp to:
- jatmon@mail.iag.net -:|:- rtfm.mit.edu /pub/usenet-by-group/....
-
-